package samples
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import marcel.display.draw.DrawManager;
	import marcel.display.gui.texts.CoreField;
	import marcel.utils.ArrayUtils;
	import marcel.utils.BitmapUtils;
	import marcel.utils.ColorUtils;
	import marcel.utils.Delegate;
	import marcel.utils.TextFieldUtils;
	
	public class TextFieldBitmapSample extends Sprite
	{
		protected var _cf:CoreField;
		
		public function TextFieldBitmapSample() {
			super();
			x = y = 300;
			_createTextField();
			_getBitmap();
		}
		
		protected function _createTextField():void {
			_cf = new CoreField(0,0,250,40);
			_cf.text = "Wen";
			_cf.setTextFormat(new TextFormat("Verdana", 30));
			addChild(_cf);
			_cf.y = 100;
		}
		
		protected function _getBitmap():void {
			var bmpd:BitmapData = TextFieldUtils.getAllWordsBitmap(_cf)[0].bitmapData;
			addChild(new Bitmap(bmpd));
			
			var nbPixel:int = bmpd.width * bmpd.height;
			var dim:Point = new Point(bmpd.width, bmpd.height);
			var p:Point;
			var aMap:Array = new Array();
			for (var i:int = 0; i < nbPixel; i++) {
				p = ArrayUtils.getShift(i,dim);
				aMap.push(ColorUtils.hexToARGB(bmpd.getPixel32(p.x, p.y)).a);
			}
			
			var map:String = "";
			map += "0110110";
			map += "1001001";
			map += "0110110";
			map += "0010100";
			map += "0001000";
			
			var _bmpd:BitmapData = DrawManager.drawPixelAsset(BitmapUtils.getPixel(0xFFFFFFFF), String("001010100").split(""), new Point(3,3));
			_bmpd = drawColoredPixelAsset(_bmpd, 0xFFFFFF, aMap, dim);
			var _bmp:Bitmap = new Bitmap(_bmpd);
			_bmp.x = 150;
			addChild(_bmp);
		}
		
		public function drawColoredPixelAsset(pattern:BitmapData, color:int, map:Array, size:Point):BitmapData {
			var _bmp:BitmapData = new BitmapData(size.x * pattern.width, size.y * pattern.height, true, 0);
			map.map(Delegate.create(__applyColoredPatternOnMap, size, pattern, _bmp, color));
			return _bmp;
		}
		
		private function __applyColoredPatternOnMap(element:int, index:int, array:Array, size:Point, pattern:BitmapData, bmp:BitmapData, color:int):void {
			if (element === 0) return;
			var _p:Point = ArrayUtils.getShift(index, size);
			var _coloredBitmap:BitmapData = pattern.clone();
			var _cu:ColorUtils = ColorUtils.hexToARGB(color);
			_coloredBitmap.colorTransform(pattern.rect, new ColorTransform(0, 0, 0, 0, _cu.r, _cu.g, _cu.b, element));
			bmp.copyPixels(_coloredBitmap, pattern.rect, new Point(_p.x * pattern.width, _p.y * pattern.height));	
		}
		
	}
}